package org.jeecg.modules.innerApi.common.convert;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.innerApi.common.requestDto.*;
import org.jeecg.modules.xxgx.common.enums.HsbzEnum;
import org.jeecg.modules.xxgx.common.enums.HzsqfEnum;
import org.jeecg.modules.xxgx.common.util.FPUtil;
import org.jeecg.modules.xxgx.common.vo.HsBhsConvertVo;
import org.jeecg.modules.xxgx.hzsqgl.vo.CsXxHzsqjsPage;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * 将页面或者接口开票相关参数转换为公用开票相关参数
 */
public class RequestParamConvert {

    /**
     * 红字申请数据转换
     */
    public static HzxxbsqParam hzxxbsqParamConvert(CsXxHzsqjsPage page){
        HzxxbsqParam param=new HzxxbsqParam();
        param.setSqlsh(page.getSqlsh());
        param.setXhdwmc(page.getXfmc());
        param.setXhdwsbh(page.getXfsh());
        param.setGhdwmc(page.getKhmc());
        param.setGhdwsbh(page.getKhsh());
        param.setKprq(DateUtils.date2Str(page.getYkprq(),DateUtils.yyyymmddhhmmss.get()));
        param.setFplxdm(page.getYfplxdm());
        //参照新时代智云传参
        param.setZdbz("2");
        String sqf=page.getSqf();
        String sqly=null;
        if(sqf.equals(HzsqfEnum.gfsq_ydk.getValue())){
            sqly="0";
        }else if(sqf.equals(HzsqfEnum.gfsq_wdk.getValue())){
            sqly="1";
        }else if(sqf.equals(HzsqfEnum.xfsq.getValue())){
            sqly="2";
        }
        param.setSqly(sqly);
        param.setSqfs(StringUtils.isNotEmpty(page.getHclx())?page.getHclx():"");
        param.setZsfs(StringUtils.isNotEmpty(page.getZsfs())?page.getZsfs():"");

        param.setQdbz(StringUtils.isNotEmpty(page.getQdbz())?page.getQdbz():"");
        param.setHjje(page.getHjje() == null ? "" : page.getHjje().toPlainString());
        param.setHjse(page.getHjse() == null ? "" : page.getHjse().toPlainString());
        param.setYfpdm(StringUtils.isNotEmpty(page.getYfpdm())?page.getYfpdm():"");
        param.setYfphm(StringUtils.isNotEmpty(page.getYfphm())?page.getYfphm():"");
        List hzfpmx=new ArrayList();
        page.getCsXxHzsqjsmxList().forEach(item->{
            HzxxbsqParam.HzxxbsqmxParam mx=new HzxxbsqParam.HzxxbsqmxParam();
            mx.setFphxz(StringUtils.isEmpty(item.getFphxz())?"0":item.getFphxz());
            mx.setSpmc(StringUtils.isNotEmpty(item.getSpmc())?item.getSpmc():"");
            mx.setSpsm(StringUtils.isNotEmpty(item.getSpbm())?item.getSpbm():"");
            mx.setGgxh(StringUtils.isNotEmpty(item.getGgxh())?item.getGgxh():"");
            mx.setDw(StringUtils.isNotEmpty(item.getJldw())?item.getJldw():"");
            mx.setSpsl(item.getSpsl()!=null?String.valueOf(item.getSpsl()):"");
            HsBhsConvertVo hsBhsConvertVo=new HsBhsConvertVo();
            BeanUtils.copyProperties(item,hsBhsConvertVo);
            FPUtil.hsBhsConvert(hsBhsConvertVo);
            //更新转换后的数据
            BeanUtils.copyProperties(hsBhsConvertVo,item);
            String hsbs=page.getHsbz();
            if(HsbzEnum.N.getValue().equals(hsbs)){//不含税
                mx.setDj(item.getBhsdj()!=null?item.getBhsdj().toPlainString():"");
                mx.setJe(item.getBhsje()!=null?item.getBhsje().toPlainString():"");
            }else{//含税
                mx.setDj(item.getHsdj()!=null?item.getHsdj().toPlainString():"");
                mx.setJe(item.getHsje()!=null?item.getHsje().toPlainString():"");
            }
            mx.setSl(item.getTax()!=null?item.getTax().toPlainString():"");
            mx.setSe(item.getTaxje()!=null?item.getTaxje().toPlainString():"");
            mx.setHsbz(StringUtils.isNotEmpty(page.getHsbz())?page.getHsbz():"");
            mx.setSpbm(StringUtils.isNotEmpty(item.getSpbm())?item.getSpbm():"");
            mx.setZxbm(StringUtils.isNotEmpty(item.getSpdm())?item.getSpdm():"");
            mx.setYhzcbs(StringUtils.isNotEmpty(item.getYhzcbs())?item.getYhzcbs():"");
            mx.setLslbs(StringUtils.isNotEmpty(item.getLslbs())?item.getLslbs():"");
            hzfpmx.add(mx);
        });
        param.setHzsqmxs(hzfpmx);
        return param;
    }


    public static void main(String[] args){
        String rq="2022-02-07T03:13:27.000+0000";
        DateUtils.str2Date(rq,DateUtils.date_sdf.get());
        System.out.println(DateUtils.formatDate(DateUtils.str2Date(rq,DateUtils.datetimeFormat.get())));
    }

}
